package edu.egypt.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import edu.egypt.helpers.PermissionsDbOperations;
import edu.egypt.model.Permission;

/**
 * Servlet implementation class role_permServlet
 */
public class role_permServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public role_permServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		ArrayList<Integer> perm = PermissionsDbOperations.getSingletonInstance().getPermissionByRole(id);
		
		ArrayList<Permission> allperm = PermissionsDbOperations.getSingletonInstance().getAllPermissions();
	//	HashMap<String, String> ret = new HashMap<>();
		
		
		for(int i = 0; i < allperm.size() ; i++){
			for(int k=0 ; k < perm.size() ; k++){
				if(perm.get(k) == allperm.get(i).getId()){
					allperm.get(i).setAssigned(true);
				}
	//			ret.put("permo",allperm.get(i).getName() +" "+ allperm.get(i).getAssigned());
			}
		}
		/*
		request.setAttribute("perms", allperm);
		RequestDispatcher dispatcher = request.getRequestDispatcher("temp.jsp");
		dispatcher.forward(request, response);
		*/
			response.setContentType("Application/json");
			PrintWriter out = response.getWriter();
			Gson gson = new Gson();
			String s = gson.toJson(allperm);
			out.println(s);
			out.flush();
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
